package com.hpsk.shopservice.model.config.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hpsk.common.RespResult;
import com.hpsk.common.RespUtils;
import com.hpsk.shopservice.model.config.dto.BizConfigMenuAddDto;
import com.hpsk.shopservice.model.config.entity.BizConfigMenu;
import com.hpsk.shopservice.model.config.service.IBizConfigMenuService;
import com.hpsk.shopservice.model.config.vo.BizConfigMenuVo;
import com.hpsk.shopservice.model.config.vo.MenuVo;
import com.hpsk.validator.AddGroup;
import com.hpsk.validator.EditGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 列表控制器 前端控制器
 * </p>
 *
 * @author huangwang
 * @since 2025-03-20
 */
@RestController
@RequestMapping("/bizConfigMenu")
@Tag(name = "菜单控制器")
@Slf4j
@AllArgsConstructor(onConstructor_ = @__(@Autowired))
@Validated
public class BizConfigMenuController {

    private final IBizConfigMenuService bizConfigMenuService;
    @Operation(summary = "添加菜单")
    @PostMapping("/add")
    public RespResult<BizConfigMenuVo> add(@RequestBody @Validated(value = {AddGroup.class}) BizConfigMenuAddDto bizConfigMenuAddDto){
        return RespUtils.ok(bizConfigMenuService.add(bizConfigMenuAddDto));
    }

    @Operation(summary = "编辑菜单")
    @PostMapping("/updateMenu")
    public RespResult<BizConfigMenuVo> updateMenu(@RequestBody @Validated(value = {EditGroup.class}) BizConfigMenuAddDto bizConfigMenuAddDto){
        return RespUtils.ok(bizConfigMenuService.updateMenu(bizConfigMenuAddDto));
    }

    @Operation(summary = "删除菜单")
    @GetMapping("/deleteMenu")
    public RespResult<String> deleteMenu(@ParameterObject @Valid Long id){
        boolean b = bizConfigMenuService.removeById(id);
        return RespUtils.ok();
    }

    @Operation(summary = "根据层级获取菜单")
    @GetMapping("/getLevelList/{level}")
    public RespResult<List<BizConfigMenu>> getLevelList(@PathVariable Integer level){
        List<BizConfigMenu> list = bizConfigMenuService.list(Wrappers.<BizConfigMenu>lambdaQuery().eq(BizConfigMenu::getLevel, level));
        return RespUtils.ok(list);
    }

    @Operation(summary = "获取菜单列表")
    @GetMapping("/getAllList")
    public RespResult<List<MenuVo>> getAllList(){
        List<MenuVo> list = bizConfigMenuService.getAllList();
        return RespUtils.ok(list);
    }

}
